package com.ruoyi.business.service.impl;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.WaterchillerhisMapper;
import com.ruoyi.business.domain.Waterchillerhis;
import com.ruoyi.business.service.IWaterchillerhisService;
import java.time.format.DateTimeFormatter;

/**
 * 水冷机历史数据Service业务层处理
 * 
 * @author DL
 * @date 2025-07-07
 */
@Service
public class WaterchillerhisServiceImpl implements IWaterchillerhisService 
{
    @Autowired
    private WaterchillerhisMapper waterchillerhisMapper;

    /**
     * 查询水冷机历史数据
     * 
     * @param ID 水冷机历史数据主键
     * @return 水冷机历史数据
     */
    @Override
    public Waterchillerhis selectWaterchillerhisByID(Long ID)
    {
        return waterchillerhisMapper.selectWaterchillerhisByID(ID);
    }

    /**
     * 查询水冷机历史数据列表
     * 
     * @param waterchillerhis 水冷机历史数据
     * @return 水冷机历史数据
     */
    @Override
    public List<Waterchillerhis> selectWaterchillerhisList(Waterchillerhis waterchillerhis)
    {
        return waterchillerhisMapper.selectWaterchillerhisList(waterchillerhis);
    }

    /**
     * 新增水冷机历史数据
     * 
     * @param waterchillerhis 水冷机历史数据
     * @return 结果
     */
    @Override
    public int insertWaterchillerhis(Waterchillerhis waterchillerhis)
    {
        return waterchillerhisMapper.insertWaterchillerhis(waterchillerhis);
    }

    /**
     * 修改水冷机历史数据
     * 
     * @param waterchillerhis 水冷机历史数据
     * @return 结果
     */
    @Override
    public int updateWaterchillerhis(Waterchillerhis waterchillerhis)
    {
        return waterchillerhisMapper.updateWaterchillerhis(waterchillerhis);
    }

    /**
     * 批量删除水冷机历史数据
     * 
     * @param IDs 需要删除的水冷机历史数据主键
     * @return 结果
     */
    @Override
    public int deleteWaterchillerhisByIDs(Long[] IDs)
    {
        return waterchillerhisMapper.deleteWaterchillerhisByIDs(IDs);
    }

    /**
     * 删除水冷机历史数据信息
     * 
     * @param ID 水冷机历史数据主键
     * @return 结果
     */
    @Override
    public int deleteWaterchillerhisByID(Long ID)
    {
        return waterchillerhisMapper.deleteWaterchillerhisByID(ID);
    }

    @Override
    public Map<String, Object> getWaterDataBetween(LocalDateTime startTime, LocalDateTime endTime) {
        // 查询数据库获取数据
        List<Waterchillerhis> dataList = waterchillerhisMapper.findByTimestampBetween(startTime, endTime);

        // 处理数据格式
        Map<String, Object> result = new HashMap<>();
        String[] xdata = new String[dataList.size()];
        double[] LowWaterTemp = new double[dataList.size()];
        double[] HighWaterTemp = new double[dataList.size()];
        double[] LowWaterRate = new double[dataList.size()];
        double[] HighWaterRate = new double[dataList.size()];

        // 定义格式化器
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

        for (int i = 0; i < dataList.size(); i++) {
            Waterchillerhis data = dataList.get(i);
            xdata[i] = sdf.format(data.getEventTime());
            LowWaterTemp[i] = data.getLowWaterTemp() != null ? data.getLowWaterTemp().doubleValue() : 0.0;
            HighWaterTemp[i] = data.getHighWaterTemp() != null ? data.getHighWaterTemp().doubleValue() : 0.0;
            LowWaterRate[i] = data.getLowWaterRate() != null ? data.getLowWaterRate().doubleValue() : 0.0;
            HighWaterRate[i] = data.getHighWaterRate() != null ? data.getHighWaterRate().doubleValue() : 0.0;
        }

        result.put("xdata", xdata);
        result.put("LowWaterTemp", LowWaterTemp);
        result.put("HighWaterTemp", HighWaterTemp);
        result.put("LowWaterRate", LowWaterRate);
        result.put("HighWaterRate", HighWaterRate);

        return result;
    }
}
